import Taro from "@tarojs/taro"

const _request = ({ method, url, params, config }) => {
    return new Promise((reslove, reject) => {
        Taro.request({
            url,
            method: method || "GET",
            data: params,
            success: (response) => {
                // 对response做简单处理，如果需要自定义在config中传入success方法重写即可
                if (response.statusCode == 200) {
                    const data = response.data
                    reslove(data)
                } else {
                    Taro.showToast({
                        title: `网络拥堵，请稍后重试: ${response.statusCode}`,
                        icon: 'none',
                        mask: true,
                    })
                }
            },
            ...config
        })
    })
}

export const POST = (url, params, config) => _request({ method: "POST", url, params, config })

export const GET = (url, params, config) => _request({ method: "GET", url, params, config })

export const PUT = (url, params, config) => _request({ method: "PUT", url, params, config })

export const DELETE = (url, params, config) => _request({ method: "DELETE", url, params, config })
